/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brain.core.configuracion;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Ronald
 */
public class SessionTimeoutCookieFilter implements Filter{

    @Override
    public void init(FilterConfig fc) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
            HttpServletResponse httpResp = (HttpServletResponse) resp;
            HttpServletRequest httpReq = (HttpServletRequest) req;
            HttpSession session = ((HttpServletRequest) httpReq).getSession(false);
            
            long currTime = System.currentTimeMillis();
            System.out.println(currTime);
            long expiryTime = currTime + session.getMaxInactiveInterval() * 1000;
            System.out.println(expiryTime);
            Cookie cookie = new Cookie("serverTime", "" + currTime);
            cookie.setPath("/");
            httpResp.addCookie(cookie);
            if (httpReq.getRemoteUser() != null) {
                cookie = new Cookie("sessionExpiry", "" + expiryTime);
            } else {
                cookie = new Cookie("sessionExpiry", "" + currTime);
            }
            
            
            cookie.setPath("/");
            httpResp.addCookie(cookie);
            filterChain.doFilter(req, resp);
	}

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
